namespace SimpleWebServices.Web.Routing
{
    using System;
    using System.Web;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using Xunit;

    public sealed class SubmissionRouteFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<SubmissionRoute>()
                .DerivesFrom<RouteBase>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new SubmissionRoute());
        }

        [Fact]
        public void op_GetRouteData_HttpContextBaseNull()
        {
            Assert.Throws<ArgumentNullException>(() => new SubmissionRoute().GetRouteData(null));
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase()
        {
            FakeRepository.Clear();

            try
            {
                Token token = Token.New();

                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://example.com/submission/" + Token.New(),
                    xml = "<submission><data /></submission>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.AppRelativeCurrentExecutionFilePath).Returns("~/test/abc/xyz/submission").Verifiable();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test/abc/xyz/submission").Verifiable();
                mock.SetupGet<string>(x => x.Request.PathInfo).Returns(string.Empty).Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/test/abc/xyz/submission?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new SubmissionRoute().GetRouteData(mock.Object);

                Assert.Equal<string>("test/abc/xyz/submission", (actual.Route as Route).Url);
                Assert.Equal<string>("Submission", (string)actual.Values["controller"]);
                Assert.Equal<string>("Process", (string)actual.Values["action"]);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetRouteData_HttpContextBase_whenTokenMissing()
        {
            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test/abc/xyz/submission").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/test/abc/xyz/submission").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RouteData actual = new SubmissionRoute().GetRouteData(mock.Object);

                Assert.Null(actual);

                mock.VerifyAll();
            }
            finally
            {
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContextNull_RouteValueDictionary()
        {
            Assert.Throws<ArgumentNullException>(() => new SubmissionRoute().GetVirtualPath(null, new RouteValueDictionary()));
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary()
        {
            FakeRepository.Clear();

            try
            {
                Token token = Token.New();

                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://example.com/submission/" + Token.New(),
                    xml = "<submission><data /></submission>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test/abc/xyz/submission").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/test/abc/xyz/submission?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                VirtualPathData actual = new SubmissionRoute().GetVirtualPath(request, new RouteValueDictionary());

                Assert.NotNull(actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_GetVirtualPath_RequestContext_RouteValueDictionary_whenTokenMissing()
        {
            FakeRepository.Clear();

            try
            {
                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/test/abc/xyz/submission").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/test/abc/xyz/submission").Verifiable();
                HttpContextFactory.Mock = mock.Object;

                RequestContext request = new RequestContext(mock.Object, new RouteData());

                VirtualPathData actual = new SubmissionRoute().GetVirtualPath(request, new RouteValueDictionary());

                Assert.Null(actual);

                mock.VerifyAll();
            }
            finally
            {
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}